﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using libs.DBUtility;//Please add references
using libs.Model;
using System.Collections.Generic;
namespace libs.DAL
{
	/// <summary>
	/// 数据访问类:setting
	/// </summary>
	public partial class settingDao
	{
		public settingDao()
		{}
		#region  BasicMethod



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(setting model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into setting(");
			strSql.Append("cardiomin,cardiomax,illumimin,illumimax,tempermin,tempermax,moisturemin,moisturemax)");
			strSql.Append(" values (");
			strSql.Append("@cardiomin,@cardiomax,@illumimin,@illumimax,@tempermin,@tempermax,@moisturemin,@moisturemax)");
			SqlParameter[] parameters = {
					new SqlParameter("@cardiomin", SqlDbType.NVarChar,50),
					new SqlParameter("@cardiomax", SqlDbType.NVarChar,50),
					new SqlParameter("@illumimin", SqlDbType.NVarChar,50),
					new SqlParameter("@illumimax", SqlDbType.NVarChar,50),
					new SqlParameter("@tempermin", SqlDbType.NVarChar,50),
					new SqlParameter("@tempermax", SqlDbType.NVarChar,50),
					new SqlParameter("@moisturemin", SqlDbType.NVarChar,50),
					new SqlParameter("@moisturemax", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.cardiomin;
			parameters[1].Value = model.cardiomax;
			parameters[2].Value = model.illumimin;
			parameters[3].Value = model.illumimax;
			parameters[4].Value = model.tempermin;
			parameters[5].Value = model.tempermax;
			parameters[6].Value = model.moisturemin;
			parameters[7].Value = model.moisturemax;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(setting model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update setting set ");
			strSql.Append("cardiomin=@cardiomin,");
			strSql.Append("cardiomax=@cardiomax,");
			strSql.Append("illumimin=@illumimin,");
			strSql.Append("illumimax=@illumimax,");
			strSql.Append("tempermin=@tempermin,");
			strSql.Append("tempermax=@tempermax,");
			strSql.Append("moisturemin=@moisturemin,");
			strSql.Append("moisturemax=@moisturemax");
			SqlParameter[] parameters = {
					new SqlParameter("@cardiomin", SqlDbType.NVarChar,50),
					new SqlParameter("@cardiomax", SqlDbType.NVarChar,50),
					new SqlParameter("@illumimin", SqlDbType.NVarChar,50),
					new SqlParameter("@illumimax", SqlDbType.NVarChar,50),
					new SqlParameter("@tempermin", SqlDbType.NVarChar,50),
					new SqlParameter("@tempermax", SqlDbType.NVarChar,50),
					new SqlParameter("@moisturemin", SqlDbType.NVarChar,50),
					new SqlParameter("@moisturemax", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.cardiomin;
			parameters[1].Value = model.cardiomax;
			parameters[2].Value = model.illumimin;
			parameters[3].Value = model.illumimax;
			parameters[4].Value = model.tempermin;
			parameters[5].Value = model.tempermax;
			parameters[6].Value = model.moisturemin;
			parameters[7].Value = model.moisturemax;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from setting ");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
			};

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public setting GetModel(string strWhere)
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 cardiomin,cardiomax,illumimin,illumimax,tempermin,tempermax,moisturemin,moisturemax from setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public setting DataRowToModel(DataRow row)
		{
			setting model=new setting();
			if (row != null)
			{
				if(row["cardiomin"]!=null)
				{
					model.cardiomin=row["cardiomin"].ToString();
				}
				if(row["cardiomax"]!=null)
				{
					model.cardiomax=row["cardiomax"].ToString();
				}
				if(row["illumimin"]!=null)
				{
					model.illumimin=row["illumimin"].ToString();
				}
				if(row["illumimax"]!=null)
				{
					model.illumimax=row["illumimax"].ToString();
				}
				if(row["tempermin"]!=null)
				{
					model.tempermin=row["tempermin"].ToString();
				}
				if(row["tempermax"]!=null)
				{
					model.tempermax=row["tempermax"].ToString();
				}
				if(row["moisturemin"]!=null)
				{
					model.moisturemin=row["moisturemin"].ToString();
				}
				if(row["moisturemax"]!=null)
				{
					model.moisturemax=row["moisturemax"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        public List<setting> GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select cardiomin,cardiomax,illumimin,illumimax,tempermin,tempermax,moisturemin,moisturemax ");
			strSql.Append(" FROM setting ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            List<setting> list = new List<setting>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    list.Add(DataRowToModel(ds.Tables[0].Rows[i]));
                }
                return list;
            }
            else
            {
                return null;
            }
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" cardiomin,cardiomax,illumimin,illumimax,tempermin,tempermax,moisturemin,moisturemax ");
			strSql.Append(" FROM setting ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM setting ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from setting T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "setting";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

